import service from "./service.js";

const headers = {}

export default (url, method, params,power) => {
	uni.showLoading({
		title: "上传中"
	});
	
	const api = service.filter(item => {
		return item.url === url;  //判断是否有对应的接口
	})
	
	// 获取token
	let isToken = uni.getStorageSync('loginStatus').token;
	if(isToken){
		headers['token'] = isToken;
	}
	
	// 给接口传递来源
	switch (power) {
		case 1:
			headers['source'] = 'web'
			// headers['Authorization'] = ''
			break;
		case 2:
			headers['source'] = 'h5'
			// headers['Authorization'] = ''
			break;
		case 3:
			headers['source'] = 'app'
			// headers['Authorization'] = ''
			break;
		default:
			headers['source'] = 'xcx'
			// headers['Authorization'] = ''
			break;
	}
	
	return new Promise((resolve,reject) => {
		uni.uploadFile({
			// url: "http://101.43.50.104//fourleaf/public/index.php" + url,
			url: "https://www.hermit.love/fourleaf/public/index.php" + url,
			filePath:params.file,
			name:'file',
			success(res) {
				let upData = JSON.parse(res.data)
				if(upData.code === 200 || upData.code === 201){
					// 成功返回数据
					resolve(upData);
				} else if(upData.code === 403){
					// 失败清除全部缓存去登录
					uni.clearStorage();
					reject(err);
				} else{
					uni.showToast({
						title:'上传失败',
						icon:'none'
					});
				}
			},
			fail(err) {
				reject(err);
			},
			complete() {
				uni.hideLoading();
			}
		})
	})
}